﻿﻿using System;
using StarTrek.Ship;
using StarTrek.World;

namespace StarTrek.Weapon
{
    class Shield:iWeapon
    {
        private float cShieldStrength;
        private float cRepairEff;
        private float cMaxStrength;
        private bool damaged;
        private string name;
        private iShip parent;

        public Shield(float ShieldStrength,float RepairEff)
        {
            cShieldStrength = ShieldStrength;
            cMaxStrength = cShieldStrength;
            cRepairEff = RepairEff;
        }
        public Shield(){}

        public float ShieldStrength
        {
            get
            {
                return cShieldStrength;
            }
        }
        
        public float ShieldPerc
        {
            get
            {
                return cShieldStrength/cMaxStrength;
            }
        }

        internal float DamageShield(float DamagePoints)
        {
            float est = cShieldStrength - DamagePoints;
            float rVal;

            if (est > 0)
            {
                cShieldStrength = est;
                rVal = 0;
            }
            else
            {
                cShieldStrength = 0;
                rVal = 0 - est;
            }

            return rVal;
        }

        #region iWepon Members

        public ShipStatus ApplyWeponToShip(iShip Target, float Energy)
        {
            throw new InvalidWeaponException(this, Target);
        }

        public iShip ApplyDefenseToShip(float Energy)
        {
            float startingStrength = cShieldStrength;
            //this is conceptually repairing the shields with engery.
            cShieldStrength += (Energy*cRepairEff);

            if (cShieldStrength <= cMaxStrength)
                cShieldStrength = cMaxStrength; //The Player did lose extra engery in this case.

            System.Diagnostics.Debug.WriteLine(String.Format("Used {0} units of engery to repair the shield {1} units - {2:#.0000} effecicent", Energy, (cShieldStrength - startingStrength), Energy / (cShieldStrength - startingStrength)));

            return this.parent;
        }

        public iShip Parent
        {
            get
            {
                return parent;
            }
            set
            {
                parent = value;
            }
        }


        public bool isFunctional
        {
            get
            {
                return !damaged;
            }
            set
            {
                damaged = !value;
            }
        }


        public string WeaponReport()
        {
            return String.Format("Shields operating at {0:###}% capacity.", Math.Round(cShieldStrength / cMaxStrength, 2) * 100);
        }

        public void Load(System.Xml.XmlNode node)
        {
            //Strength="2000"
            cShieldStrength = Convert.ToSingle(node.Attributes["Strength"].Value);
            cMaxStrength = cShieldStrength;
            cRepairEff = Convert.ToSingle(node.Attributes["Strength"].Value);
            name = node.Attributes["Name"].Value;
        }

        public bool isOffensive { get { return false; } }
        public bool NeedsEnegery { get { return true; } } 
        public string Name {get { return name;}}
        #endregion
    }
}
